package org.dromara.system.controller.system;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysAreaVo;
import org.dromara.system.domain.bo.SysAreaBo;
import org.dromara.system.service.ISysAreaService;

/**
 * 系统管理-区域配置
 * 前端访问路由地址为:/system/area
 *
 * @author LionLi
 * @date 2024-03-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController {

    private final ISysAreaService sysAreaService;

    /**
     * 查询区域树形结构-区域配置列表
     */
//    @SaCheckPermission("system:area:tree")
    @GetMapping("/tree")
    public R<List<Tree<Long>>> tree(SysAreaBo bo) {
        List<Tree<Long>> tree = sysAreaService.tree(bo);
        return R.ok(tree);
    }

    /**
     * 导出系统管理-区域配置列表
     */
    @SaCheckPermission("system:area:export")
    @Log(title = "系统管理-区域配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAreaBo bo, HttpServletResponse response) {
        List<SysAreaVo> list = sysAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统管理-区域配置", SysAreaVo.class, response);
    }

    /**
     * 获取系统管理-区域配置详细信息
     *
     * @param areaId 主键
     */
//    @SaCheckPermission("system:area:query")
    @GetMapping("/{areaId}")
    public R<SysAreaVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long areaId) {
        return R.ok(sysAreaService.queryById(areaId));
    }

    /**
     * 新增系统管理-区域配置
     */
    @SaCheckPermission("system:area:add")
    @Log(title = "系统管理-区域配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(sysAreaService.insertByBo(bo));
    }

    /**
     * 修改系统管理-区域配置
     */
    @SaCheckPermission("system:area:edit")
    @Log(title = "系统管理-区域配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(sysAreaService.updateByBo(bo));
    }

    /**
     * 删除系统管理-区域配置
     *
     * @param areaId 主键串
     */
    @SaCheckPermission("system:area:remove")
    @Log(title = "系统管理-区域配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaId}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String areaId) {
        //判断是否存在子集合
        long areaIdLong = Long.parseLong(areaId);
        if (sysAreaService.hasChildByAreaId(areaIdLong)) {
            return R.warn("存在下级区域,不允许删除");
        }
        //判断区域下是否存在门店
        if (sysAreaService.hasStoreByAreaId(areaIdLong)) {
            return R.warn("存在门店,不允许删除");
        }
        return toAjax(sysAreaService.deleteWithValidById(areaIdLong));
    }
}
